#!/bin/bash
# 定义变量：数据同步日期，表示同步哪一条日志数据
# 第1、执行shell脚本时，传递参数
# 第2、如果没有传递参数，同步前一天数据
# todo 大数据-电商数仓-07-商品主题商品诊断看板
if [ -n "$1" ] ; then
data_date=$1
else
data_date=`date -d "-1 days" +%F`
fi
# 加载数据语句
DIM_MD_AREAS_SQL="
USE product_order;
WITH t1 AS
(
    SELECT * FROM product_order.ods_user_action_log WHERE dt = '${data_date}'
),t2 AS
(
    SELECT * FROM product_order.ods_order_origin WHERE dt = '${data_date}'
),t3 AS
(
    SELECT * FROM product_order.ods_comment_info WHERE dt between '${data_date}' and DATE_ADD('${data_date}', 7)
)
INSERT OVERWRITE TABLE product_order.dwd_product_trade_analysis PARTITION (dt='${data_date}')
SELECT
    ua.product_id,
    ua.user_id,
    oo.order_id,
    ci.review_id,
    ua.behavior_type,
    ua.channel,
    ua.province,
    ua.is_new_buyer,
    oo.order_amount,
    oo.payment_amount,
    oo.discount_amount,
    oo.order_status,
    oo.payment_type,
    ua.visit_time,
    oo.create_time,
    oo.update_time,
    ci.score,
    ci.images,
    ci.review_time,
    ci.appraise
FROM t1 ua
LEFT JOIN t2 oo ON ua.product_id = oo.product_id
LEFT JOIN t3 ci ON oo.order_id = ci.order_id;
"
# 执行SQL语句
/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${DIM_MD_AREAS_SQL}"